%{
For FBHALE software
Copyright (C) 2018-present Facebook, Inc.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
%}
  
function [ optim ] = WritePods( optim, aswingIn, useStructure, relfectionY_m, beamNumber, entityName, index  )
% Writes pod(s) to .asw file

fprintf(aswingIn,'\n');

fprintf(aswingIn,'#============\n');
fprintf(aswingIn,['Beam ' num2str(beamNumber) '\n']);
fprintf(aswingIn,[entityName '\n']);
fprintf(aswingIn,'#\n');

fprintf(aswingIn,'\n');
fprintf(aswingIn,'    t       x     y    z   radius     mg  Cea  Nea  Cta  Nta  mgcc  Ccg  Ncg\n');

%pod geometric properties
for i=1:length(optim.pod.omlSectionsR_m{index})

    fprintf(aswingIn,'\t%3.4f\t%3.4f\t%3.4f\t%3.4f\t%3.4f\t%3.4f\t%3.4f\t%3.4f\t%3.4f\t%3.4f\t%3.4f\t%3.4f\t%3.4f\n', ...
    (optim.pod.omlSectionsX_m{index}(i)-optim.pod.omlSectionsX_m{index}(1))/ optim.pod.structure.length_m(index), ...
    optim.pod.globalx_m{index}(i), ...
    relfectionY_m*optim.pod.globaly_m{index}(i), ...
    optim.pod.globalz_m{index}(i),...
    optim.pod.omlSectionsR_m{index}(i), ...
    interp1(optim.pod.structure.sRefY_m{index}, optim.pod.structure.mg_N_m{index},  optim.pod.omlSectionsX_m{index}(i),'linear','extrap'), ...
    interp1(optim.pod.structure.sRefY_m{index}, optim.pod.structure.Cea_m{index},   optim.pod.omlSectionsX_m{index}(i),'linear','extrap'),  ...
    interp1(optim.pod.structure.sRefY_m{index}, optim.pod.structure.Nea_m{index},   optim.pod.omlSectionsX_m{index}(i),'linear','extrap'), ...
    interp1(optim.pod.structure.sRefY_m{index}, optim.pod.structure.Cta_m{index},   optim.pod.omlSectionsX_m{index}(i),'linear','extrap'), ...
    interp1(optim.pod.structure.sRefY_m{index}, optim.pod.structure.Nta_m{index},   optim.pod.omlSectionsX_m{index}(i),'linear','extrap'), ...
    interp1(optim.pod.structure.sRefY_m{index}, optim.pod.structure.mgcc_Nm{index}, optim.pod.omlSectionsX_m{index}(i),'linear','extrap'),...
    interp1(optim.pod.structure.sRefY_m{index}, optim.pod.structure.Ccg_m{index},   optim.pod.omlSectionsX_m{index}(i),'linear','extrap'),  ...
    interp1(optim.pod.structure.sRefY_m{index}, optim.pod.structure.Ncg_m{index},   optim.pod.omlSectionsX_m{index}(i),'linear','extrap'));
   
end

% pod structural properties
if useStructure(4) == 0
    %don't print structural properties
else 
    fprintf(aswingIn,'\n');
    fprintf(aswingIn,'    t       EIcc     EInn     GJ  EA   EIcn   EIcs  EIsn  Cea  Nea  Cta  Nta  mgcc  Ccg  Ncg\n');
    for i=1:length(optim.pod.omlSectionsX_m{index})
        fprintf(aswingIn,'\t%3.4f\t%3.4f\t%3.4f\t%3.4f\t%3.4f\t%3.4f\t%3.4f\t%3.4f\t%3.4f\t%3.4f\t%3.4f\t%3.4f\t%3.4f\t%3.4f\t%3.4f\n', ...
            optim.pod.omlSectionsX_m{index}(i)/ optim.pod.omlSectionsX_m{index}(end), ...
            interp1(optim.pod.structure.sRefY_m{index}, optim.pod.structure.EIcc_Nm2{index},  optim.pod.omlSectionsX_m{index}(i),'linear','extrap'), ...
            interp1(optim.pod.structure.sRefY_m{index}, optim.pod.structure.EInn_Nm2{index},  optim.pod.omlSectionsX_m{index}(i),'linear','extrap'), ...
            interp1(optim.pod.structure.sRefY_m{index}, optim.pod.structure.GJ_Nm2{index},    optim.pod.omlSectionsX_m{index}(i),'linear','extrap'), ...
            interp1(optim.pod.structure.sRefY_m{index}, optim.pod.structure.EA_N{index},      optim.pod.omlSectionsX_m{index}(i),'linear','extrap'), ...
            interp1(optim.pod.structure.sRefY_m{index}, optim.pod.structure.EIcn_Nm2{index},  optim.pod.omlSectionsX_m{index}(i),'linear','extrap'), ...
            interp1(optim.pod.structure.sRefY_m{index}, optim.pod.structure.EIcs_Nm2{index},  optim.pod.omlSectionsX_m{index}(i),'linear','extrap'), ...
            interp1(optim.pod.structure.sRefY_m{index}, optim.pod.structure.EIsn_Nm2{index},  optim.pod.omlSectionsX_m{index}(i),'linear','extrap'), ...
            interp1(optim.pod.structure.sRefY_m{index}, optim.pod.structure.Cea_m{index},     optim.pod.omlSectionsX_m{index}(i),'linear','extrap'), ...
            interp1(optim.pod.structure.sRefY_m{index}, optim.pod.structure.Nea_m{index},     optim.pod.omlSectionsX_m{index}(i),'linear','extrap'), ...
            interp1(optim.pod.structure.sRefY_m{index}, optim.pod.structure.Cta_m{index},     optim.pod.omlSectionsX_m{index}(i),'linear','extrap'), ...
            interp1(optim.pod.structure.sRefY_m{index}, optim.pod.structure.Nta_m{index},     optim.pod.omlSectionsX_m{index}(i),'linear','extrap'), ...
            interp1(optim.pod.structure.sRefY_m{index}, optim.pod.structure.mgcc_Nm{index},   optim.pod.omlSectionsX_m{index}(i), 'linear','extrap'), ...
            interp1(optim.pod.structure.sRefY_m{index}, optim.pod.structure.Ccg_m{index},     optim.pod.omlSectionsX_m{index}(i),'linear','extrap'), ...
            interp1(optim.pod.structure.sRefY_m{index}, optim.pod.structure.Ncg_m{index},     optim.pod.omlSectionsX_m{index}(i),'linear','extrap'));
    end
end

fprintf(aswingIn,'End\n');

end